package SystemControl;

import java.awt.Color;
import java.io.IOException;
import java.util.List;

import javax.swing.plaf.SliderUI;
import javax.xml.bind.Marshaller.Listener;

import Analysis.*;
import Communication.ECUManager;
import Communication.MesureDistanceSensorManager;
import Communication.ReceiveSensorManager;
import Communication.SendSensorManager;
import Communication.UIPanelManager;
import Data.CarDataModel;
import UserInterface.*;
import Setting.*;
import UserInterface.UI;
import VirtualMachines.ECU;
import VirtualMachines.MesureDistanceSensor;
import VirtualMachines.UIPanel;

interface UIListener {
	void Listener(int rate, int distance);
}

public class SystemManager {
	
	CommunicationManager comManager;
	private static boolean isCompute;
	
	public SystemManager() {
		comManager = new CommunicationManager();
		isCompute = false;
	}
	public static void add_user() throws IOException {
		SettingData.addUserList();

	}

	public int get_number_of_user() {
		//return SettingData.getUserList();
		return 0;
	}

	public static void set_distance_rate(int current_user, String distance, String sound) {
		int edit_distance = Integer.parseInt(distance);
		int edit_sound = Integer.parseInt(sound);
		SettingData.data.get(current_user)[0] = edit_distance;
		System.out.println("edit Distance :" + edit_distance + "\tmodified data :" +SettingData.data.get(current_user)[0] );
		SettingData.set_sound_distance(current_user, edit_distance, edit_sound);
	}
	
	static public void compute_risk(){
		isCompute = true;
		new Thread(new Runnable() {
			@Override
			public void run() {
				while(isCompute){
					try{
						List<CarDataModel> carList = CommunicationManager.collectDataFromManagers();
						CarDataModel currentCar = carList.get(carList.size()-1);
						CarDataModel frontCar = carList.get(carList.size()-2);
						
						int risk = ComputeRisk.compute_data(currentCar, frontCar, SettingData.data.get(SettingData.current_user-1)[0]);
//						System.out.println("rate = " + SettingData.data.get(SettingData.current_user)[0]);
						
						//Normal
						if(risk == 1) {
							UIPanel.user_state = "SAFE";
							UI.user_state.setBackground(Color.green);
							CommunicationManager.sendSignalToECU(false);
						}
						//Care
						else if(risk == 2) {
							UIPanel.user_state = "NORMAL";
							UI.user_state.setBackground(Color.YELLOW);
							CommunicationManager.sendSignalToECU(false);
						}
						//Warning
						else if(risk == 3) {
							UIPanel.user_state = "WARNING";
							UI.user_state.setBackground(Color.ORANGE);
							CommunicationManager.sendSignalToECU(false);
						}
						//Danger
						else if( risk == 4) {
							UIPanel.user_state = "DANGER";
							UI.user_state.setBackground(Color.RED);
							CommunicationManager.sendSignalToECU(true);
						}
						UIPanel.distance = currentCar.distanceToFront;
	//					System.out.println(UIPanel.distance);
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}catch (Exception e) {
						// TODO: handle exception
					}
				}
			}
		}).start();
	}
	
	static public void stop_compute(){
		isCompute = false;
		UIPanel.user_state = "STOP";
	}
}

